package com.wanghlme.leetcodecn;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>剑指 Offer 35. 复杂链表的复制</p>
 * <p>JZ35->main0138</p>
 *
 * @author wanghlme
 * @version 1.0
 */
public class LC_JZ35 {
    public static void main(String[] args) {

    }

    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

    class Solution {
        public Node copyRandomList(Node head) {
            if (head == null) return head;
            Map<Node, Node> map = new HashMap<>();
            Node node = head;
            while (node != null) {
                Node clone = new Node(node.val);
                map.put(node, clone);
                node = node.next;
            }
            node = head;
            while (node != null) {
                map.get(node).next = map.get(node.next);
                map.get(node).random = map.get(node.random);
                node = node.next;
            }
            return map.get(head);
        }
    }
}
